package com.taobao.wolfwolf.web.servler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ProductSchedule extends HttpServlet {
	private List<String[]> orderList;

	private static final long serialVersionUID = 7180060183287071301L;

	/**
	 * Constructor of the object.
	 */
	public ProductSchedule() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String[] tids = request.getParameterValues("tid");
		
		@SuppressWarnings("unchecked")
		List<Object[]> tradeCollect = (List<Object[]>)request.getSession().getAttribute("tradeCollect");
		
		
		if(tids != null || tradeCollect != null)
			showOrder(tids, tradeCollect);
		
		request.getSession(true).setAttribute("orderList", orderList);
		response.sendRedirect("../scheduleVeiw.jsp");
		
	}
	
	private boolean tidsContainsTid(String[] tids, String tid) {
		for(String string : tids) {
			if(string.equals(tid))
				return true;
		}
		return false;
	}
	
	private void showOrder(String[] tids, List<Object[]> tradeCollect) {
		orderList = new ArrayList<String[]>();

		
		for(Object[] trades : tradeCollect) {
			if(tidsContainsTid(tids, String.valueOf(trades[0]))) {
				@SuppressWarnings("unchecked")
				List<String[]> orders_list = (List<String[]>) trades[3];
				
				for(String[] list : orders_list) {
					String[] order = new String[5];
					order[0] = list[5];
					order[1] = list[0];
					order[2] = list[2];
					order[3] = list[4];
					order[4] = (String) trades[7];
					orderList.add(order);
				}
			}
		}
	}

	public void init() throws ServletException {
	}

}
